'use strict';

import Base from './base.js';

export default class extends Base {
	/**
	 * index action
	 * @return {Promise} []
	 */
	indexAction() {
		//auto render template file index_index.html
		return this.display();
	}


	/**
	 * 登录接口
	 * login action
	 * @returns {Promise.<void>}
	 */
	async loginAction() {

		let userAccount = this.post( 'userAccount' );
		let userPassword = think.md5( this.post( 'userPassword' ) );
		let data = await this.model( 'index' ).findUserModel( {
			userAccount: userAccount,
			userPassword: userPassword
		} );
		if ( !think.isEmpty( data ) ) {

			this.session( 'userInfo', data.userID );
			return this.json( { msg: '登录成功', code: 1, data: data } );
		} else {
			return this.fail( { msg: '登录失败', code: 0 } )
		}
	}

	/**
	 * 注册接口
	 * register action
	 * @returns {Promise.<void>}
	 */
	async registerAction() {
		let params = this.post();
		let data = await this.model( 'index' ).registerUserModel( params );
		if ( data ) {
			return this.json( { msg: '注册成功', code: 1 } )
		} else {
			return this.json( { msg: '注册失败', code: 0 } )
		}
	}

	/**
	 * 注销接口
	 * @returns {Promise.<void>}
	 */
	async logoutAction() {
		let userInfo = await  this.session( 'userInfo' );
		if ( userID in userInfo ) {
			return this.json( { msg: '注销成功', code: 1 } )
		} else {
			return this.json( { msg: '注销失败', code: 1 } )
		}
	}

	/**
	 * 查找用户
	 * @returns {Promise.<*|{type, alias, describe}|Promise|void|PreventPromise>}
	 */
	async findUserAction() {
		let list = await this.model( 'index' ).findUserListsModel();

		return !think.isEmpty( list ) ? this.json( { msg: '成功', code: 1, data: list } ) : this.json( {
			msg: '失败',
			code: 0,
			data: []
		} )

	}

	/**
	 * 删除用户
	 * @returns {Promise.<*|{type, alias, describe}|Promise|void|PreventPromise>}
	 */
	async deleteUserAction() {
		let userID = this.post( 'userID' );
		let del = await  this.model( 'index' ).deleteUserModel( userID );
		return del === 1 ? this.json( { msg: '删除成功', code: 1 } ) : this.json( { msg: '删除失败', code: 0 } );

	}

	/**
	 * 添加文章分类
	 * @returns {Promise.<void>}
	 */
	async addArticleTypeAction() {
		let artTypeName = this.post( 'artTypeName' );
		let artTypeDesc = this.post( 'artTypeDesc' );
		let params = {
			artTypeID: think.md5( Date.now() ),
			artTypeName: artTypeName,
			artTypeDesc: artTypeDesc
		};

		if ( artTypeName !== '' ) {
			let addArt = await  this.model( 'index' ).addArticleTypeModel( params );
			return this.json( { msg: '成功', code: 1 } )
		} else {
			return this.json( { msg: '失败', code: 0 } )
		}

	}

	/**
	 * 查找文章文类
	 * @returns {Promise.<Promise|*|void|PreventPromise|{type, alias, describe}>}
	 */
	async getArticleTypeAction() {
		let getArt = await  this.model( 'index' ).getArticleTypeModel();

		return !think.isEmpty( getArt ) ? this.json( { msg: '成功', code: 1, data: getArt } ) : this.json( {
			msg: '失败',
			code: 0,
			data: []
		} )
	}

	/**
	 * 重置密码
	 * @returns {Promise.<*|PreventPromise|void|Promise|{type, alias, describe}>}
	 */
	async resetUserPwdAction() {
		let id = this.post( 'userID' );
		let getArt = await  this.model( 'index' ).resetUserPwdModel( id );

		return getArt === 1 ? this.json( { msg: '成功', code: 1 } ) : this.json( { msg: '失败', code: 1 } );

	}
}